home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / CHAR.C < prev    next >
C/C++ Source or Header  |  1991-10-29  |  4KB  |  150 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/users/jinx/microcode/RCS/char.c,v 9.29 1991/10/29 22:55:11 jinx Exp $
  4.  
  5. Copyright (c) 1987-1991 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Character primitives. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include <ctype.h>
  40.  
  41. long
  42. DEFUN (arg_ascii_char, (n), int n)
  43. {
  44.   CHECK_ARG (n, CHARACTER_P);
  45.   {
  46.     fast SCHEME_OBJECT object = (ARG_REF (n));
  47.     if (! (CHAR_TO_ASCII_P (object)))
  48.       error_bad_range_arg (n);
  49.     return (CHAR_TO_ASCII (object));
  50.   }
  51. }
  52.  
  53. long
  54. DEFUN (arg_ascii_integer, (n), int n)
  55. {
  56.   return (arg_index_integer (n, MAX_ASCII));
  57. }
  58.  
  59. DEFINE_PRIMITIVE ("MAKE-CHAR", Prim_make_char, 2, 2, 0)
  60. {
  61.   PRIMITIVE_HEADER (2);
  62.   PRIMITIVE_RETURN
  63.     (MAKE_CHAR ((arg_index_integer (2, MAX_BITS)),
  64.         (arg_index_integer (1, MAX_CODE))));
  65. }
  66.  
  67. DEFINE_PRIMITIVE ("CHAR-BITS", Prim_char_bits, 1, 1, 0)
  68. {
  69.   PRIMITIVE_HEADER (1);
  70.   CHECK_ARG (1, CHARACTER_P);
  71.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (CHAR_BITS (ARG_REF (1))));
  72. }
  73.  
  74. DEFINE_PRIMITIVE ("CHAR-CODE", Prim_char_code, 1, 1, 0)
  75. {
  76.   PRIMITIVE_HEADER (1);
  77.   CHECK_ARG (1, CHARACTER_P);
  78.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (CHAR_CODE (ARG_REF (1))));
  79. }
  80.  
  81. DEFINE_PRIMITIVE ("CHAR->INTEGER", Prim_char_to_integer, 1, 1, 0)
  82. {
  83.   PRIMITIVE_HEADER (1);
  84.   CHECK_ARG (1, CHARACTER_P);
  85.   PRIMITIVE_RETURN
  86.     (LONG_TO_UNSIGNED_FIXNUM ((ARG_REF (1)) & MASK_MIT_ASCII));
  87. }
  88.  
  89. DEFINE_PRIMITIVE ("INTEGER->CHAR", Prim_integer_to_char, 1, 1, 0)
  90. {
  91.   PRIMITIVE_HEADER (1);
  92.   PRIMITIVE_RETURN
  93.     (MAKE_OBJECT (TC_CHARACTER, (arg_index_integer (1, MAX_MIT_ASCII))));
  94. }
  95.  
  96. long
  97. DEFUN (char_downcase, (c), fast long c)
  98. {
  99.   return ((isupper (c)) ? ((c - 'A') + 'a') : c);
  100. }
  101.  
  102. long
  103. DEFUN (char_upcase, (c), fast long c)
  104. {
  105.   return ((islower (c)) ? ((c - 'a') + 'A') : c);
  106. }
  107.  
  108. DEFINE_PRIMITIVE ("CHAR-DOWNCASE", Prim_char_downcase, 1, 1, 0)
  109. {
  110.   PRIMITIVE_HEADER (1);
  111.   CHECK_ARG (1, CHARACTER_P);
  112.   PRIMITIVE_RETURN
  113.     (MAKE_CHAR ((CHAR_BITS (ARG_REF (1))),
  114.         (char_downcase (CHAR_CODE (ARG_REF (1))))));
  115. }
  116.  
  117. DEFINE_PRIMITIVE ("CHAR-UPCASE", Prim_char_upcase, 1, 1, 0)
  118. {
  119.   PRIMITIVE_HEADER (1);
  120.   CHECK_ARG (1, CHARACTER_P);
  121.   PRIMITIVE_RETURN
  122.     (MAKE_CHAR ((CHAR_BITS (ARG_REF (1))),
  123.         (char_upcase (CHAR_CODE (ARG_REF (1))))));
  124. }
  125.  
  126. DEFINE_PRIMITIVE ("ASCII->CHAR", Prim_ascii_to_char, 1, 1, 0)
  127. {
  128.   PRIMITIVE_HEADER (1);
  129.   PRIMITIVE_RETURN (ASCII_TO_CHAR (arg_index_integer (1, MAX_ASCII)));
  130. }
  131.  
  132. DEFINE_PRIMITIVE ("CHAR->ASCII", Prim_char_to_ascii, 1, 1, 0)
  133. {
  134.   PRIMITIVE_HEADER (1);
  135.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (arg_ascii_char (1)));
  136. }
  137.  
  138. DEFINE_PRIMITIVE ("CHAR-ASCII?", Prim_char_ascii_p, 1, 1, 0)
  139. {
  140.   PRIMITIVE_HEADER (1);
  141.   CHECK_ARG (1, CHARACTER_P);
  142.   {
  143.     fast SCHEME_OBJECT character = ARG_REF (1);
  144.     PRIMITIVE_RETURN
  145.       (((OBJECT_DATUM (character)) >= MAX_ASCII) ?
  146.        SHARP_F :
  147.        (LONG_TO_UNSIGNED_FIXNUM (CHAR_TO_ASCII (character))));
  148.   }
  149. }
  150.